package com.action.background;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.enetiy.Urls;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.UrlService;
import com.util.IPUtil;


public class GuanLiUrlAction extends ActionSupport {
	/*String taget="";
	private UrlService urlService; 
	private int id;
	private Urls u;
	private int [] ids;//文字id
	private int type;//页面
	
	private File file;//文件
	private String fileContextType;
	
	//登陆界面图片
	public String loginImg(){	
		Map<String, Object> request = ActionContext.getContext().getContextMap();
		String path = IPUtil.getFilePath()+"/img/loginImg/loginImg";			
		File f = new File(path);//拿到图片所在的文件夹
		String[] list = f.list();//拿到图片
		if(list.length>0){
			String img = list[0];
			request.put("img", img);
		}else{
			request.put("img","");
		}
		return "loginimg";
	}
	
	//修改登陆界面图片
	public String updateLoginImg(){
		try {
			if(file!=null){
				String path = IPUtil.getFilePath()+"/img/loginImg/loginImg";			
				File f = new File(path,u.getPic());//拿到图片所在的文件夹
				boolean d = f.delete();
				if(d){
					SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
					String FileName = sdf.format(new Date());
					FileName = FileName+".png";//创建文件名
					File fi = new File(path,FileName);
						FileUtils.copyFile(file, fi);
						return "updateLoginImg";
					}else{
						return "err";
					}
				
			}else{
				return "err";
			}
		} catch (IOException e) {
			e.printStackTrace();
			return "err";
		}
	}
	
	//添加登陆界面图片
	public String insertLoginImg(){
		try {
			String path = IPUtil.getFilePath()+"/img/loginImg/loginImg";			
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
			String FileName = sdf.format(new Date());
			FileName = FileName+".png";//创建文件名
			File fi = new File(path,FileName);
				FileUtils.copyFile(file, fi);
				return "insertLoginImg";
		} catch (IOException e) {
			e.printStackTrace();
			return "err";
		}
	}
	
	public String deleteLoginImg(){
		String path = IPUtil.getFilePath()+"/loginImg";			
		File f = new File(path,u.getPic());//拿到图片所在的文件夹
		if( f.delete()){
			return "deleteLoginImg";
		}else{
			return "err";
		}
	}
	
	//第一个请求的内容
	public String request1(){
		Map<String, Object> request = ActionContext.getContext().getContextMap();
		try {
			List<Urls> url1 = urlService.sleUrlsList(1, 1);
			request.put("req1", url1);
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
		return "request1";
	}
	//第二个请求的内容
	public String request2(){
		Map<String, Object> request = ActionContext.getContext().getContextMap();
		try {
			List<Urls> url1 = urlService.sleUrlsList(1, 2);
			request.put("req2", url1);
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
		return "request2";
	}
	//第三个请求的内容
	public String request3(){
		Map<String, Object> request = ActionContext.getContext().getContextMap();
		try {
			List<Urls> url3 = urlService.sleUrlsList(1, 3);
			request.put("req3", url3);
			List<Urls> url4 = urlService.sleUrlsList(1, 4);
			request.put("req4", url4);
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
		return "request3";
	}
	//拿到一条数据
	public String getUrl(){
		try {
			Urls url = urlService.sleUrlsId(id);
			taget="{\"text\":\""+url.getText()+"\""
					+",\"url\":\""+url.getUrl()+"\""
					+",\"page\":\""+url.getPage()+"\""
					+",\"rid\":\""+url.getRid()+"\""
					+",\"pic\":\""+url.getPic()+"\""
					+",\"id\":\""+url.getId()+"\"}";
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
		return "getUrl";
	}

	//修改文字
	public String updateUrl(){	
		try {
			
			Urls ur = urlService.sleUrlsId(id);
			urlService.updateUrl(ur);//保存时间
			urlService.updateUrl(u);//保存文字	
			taget="修改成功";
		} catch (Exception e) {
			taget="未修改";
			e.printStackTrace();
		}
		return "updeteUrl";
	}
	
	//删除文字
	public String deleteTextUrl(){
		if(ids!=null){
			for (int i = 0; i < ids.length; i++) {
				try {
					Urls s = urlService.sleUrlsId(ids[i]);
					urlService.deleteUrl(s);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return "deleteTextUrl";
		}
		return "deleteTextUrl";
	}
	//添加文字
	public String insertTextUrl(){
		Urls url =new Urls();
		url.setPage(1);
		url.setRid(1);
		url.setText("");
		try {
		for (int i = 0; i < 5; i++) {		
				urlService.insertUrl(url);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "insertTextUrl";
	}
	
	//修改图片
	public String updateImgUrl(){
		if(file!=null){//如file不为空
			String pic = u.getPic();
			String path = IPUtil.getFilePath()+"/icon/img1";
			File f = new File(path, pic);
			boolean d = f.delete();
			if(d){
				SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
				String FileName = sdf.format(new Date());
				FileName = FileName+".png";//创建文件名
				File fi = new File(path,FileName) ;//创建空文件
				try {
					FileUtils.copyFile(file, fi);
					u.setPic(FileName);
					urlService.updateUrl(u);
					if(type==1){
						
						return "updateImgUrl1";
					}else if(type==2){
						return "updateImgUrl2";
					}else{
						return "err";
					}
				} catch (IOException e) {
					fi.delete();
					e.printStackTrace();
					return "err";
				} catch (Exception e) {
					fi.delete();
					e.printStackTrace();
					return "err";
				}
			}else{
				return "err";
			}
		}else{
			try {
				urlService.updateUrl(u);
				if(type==1){
					return "updateImgUrl1";
				}else if(type==2){
					return "updateImgUrl2";
				}else{
					return "err";
				}
			} catch (Exception e) {
				e.printStackTrace();
				return "err";
			}
		}
	}
	//添加图片
	public String insertImgUrl(){
		try {
			String path = IPUtil.getFilePath()+"/icon/img1";
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
			String FileName = sdf.format(new Date());
			FileName = FileName+".png";//创建文件名
			File fi = new File(path,FileName) ;//创建空文件
			if(fi.createNewFile()){
				FileUtils.copyFile(file, fi);
				u.setPic(FileName);
				urlService.insertUrl(u);
				if(type==1){
					return "insertImgUrl1";
				}else{
					return "insertImgUrl2";
				}
			}else{
				return "err";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
	}
	//删除图片
	public String deleteImgUrl(){
		try {
			String pic = u.getPic();
			String path = IPUtil.getFilePath()+"/icon/img1";
			File f = new File(path, pic);
			boolean d = f.delete();
			if(d){
				urlService.deleteUrl(u);
				}else{
					return "err";
				}
			if(type==1){
				return "deleteImgUrl1";	
			}else if(type==2){
				return "deleteImgUrl2";	
			}else{
				return "err";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
	}
	
	 * 
	 * get/set
	 * 
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public Urls getU() {
		return u;
	}

	public void setU(Urls u) {
		this.u = u;
	}

	public UrlService getUrlService() {
		return urlService;
	}
	public void setUrlService(UrlService urlService) {
		this.urlService = urlService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTaget() {
		return taget;
	}

	public void setTaget(String taget) {
		this.taget = taget;
	}
	public int[] getIds() {
		return ids;
	}
	public void setIds(int[] ids) {
		this.ids = ids;
	}
	public String getFileContextType() {
		return fileContextType;
	}
	public void setFileContextType(String fileContextType) {
		this.fileContextType = fileContextType;
	}
	*/
}
